package com.example.smart_city_0829.activity.government;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.adapter.BaseViewPagerAdapter;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.gov.GovAdvList;
import com.example.smart_city_0829.entity.gov.GovBanner;
import com.example.smart_city_0829.entity.gov.GovType;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.example.smart_city_0829.view.MaxSizeLv;
import com.example.smart_city_0829.view.MaxSizeRv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class GovernmentMainActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private Banner banner;
    private MaxSizeGv typeGv1;
    private MaxSizeGv typeGv2;
    private MaxSizeLv mineLv;
    private MaxSizeRv rv;
    private RadioButton rb1;
    private RadioButton rb2;

    private ViewPager vp;


    private List<GovBanner.DataBean> bannerData;
    private List<GovType.RowsBean> typeData;
    private List<GovAdvList.RowsBean> myAdvData;
    private MyAdvLvAdapter advLvAdapter;

    private int pageNum = 1;

//    private TypeGvAdapter gvAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_government_main;
    }

    @Override
    protected void initView() {
        banner = (Banner) findViewById(R.id.banner);
        vp = (ViewPager) findViewById(R.id.vp);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);

//        rv = (MaxSizeRv) findViewById(R.id.rv);

        mineLv = (MaxSizeLv) findViewById(R.id.mine_lv);

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setDelayTime(3000);
    }

    @Override
    protected void initData() {
        typeGv1 = new MaxSizeGv(this);
        typeGv1.setNumColumns(4);
        typeGv2 = new MaxSizeGv(this);
        typeGv2.setNumColumns(4);

        final BaseViewPagerAdapter vpAdapter = new BaseViewPagerAdapter(Arrays.asList(typeGv1, typeGv2), this);
        vp.setAdapter(vpAdapter);

//        rv.setLayoutManager(new GridLayoutManager(this, 4));

        api.governmentMainBannerData().enqueue(new NetCallBack<GovBanner>() {
            @Override
            public void onResponse(@NonNull Call<GovBanner> call, @NonNull Response<GovBanner> response) {
                if (response.body() == null) return;
                bannerData = response.body().getData();

                final List<String> urls = bannerData.stream()
                        .map(dataBean -> Constant.BASE_URL + dataBean.getImgUrl())
                        .collect(Collectors.toList());
                banner.setImages(urls);
                final List<String> titles = bannerData.stream()
                        .map(GovBanner.DataBean::getTitle)
                        .collect(Collectors.toList());
                banner.setBannerTitles(titles);
                banner.start();
                banner.startAutoPlay();
            }
        });

        api.getGovAdvType().enqueue(new NetCallBack<GovType>() {
            @Override
            public void onResponse(Call<GovType> call, @NonNull Response<GovType> response) {
                if (response.body() == null) return;
                typeData = response.body().getRows();

                typeGv1.setAdapter(new TypeGvAdapter(typeData.subList(0, typeData.size() / 2), GovernmentMainActivity.this));
                typeGv2.setAdapter(new TypeGvAdapter(typeData.subList(typeData.size() / 2, typeData.size()), GovernmentMainActivity.this));

//                if (gvAdapter == null){
//                    gvAdapter = new TypeGvAdapter(typeData,GovernmentMainActivity.this);
//                    typeGv1.setAdapter(gvAdapter);
//                }else {
//                    gvAdapter.resetData(typeData);
//                    gvAdapter.notifyDataSetChanged();
//                }
            }
        });

        api.getGovMyAdvList(Constant.pageSize, pageNum).enqueue(new NetCallBack<GovAdvList>() {
            @Override
            public void onResponse(Call<GovAdvList> call, @NonNull Response<GovAdvList> response) {
                if (response.body() == null) return;
                myAdvData = response.body().getRows();
                //排序
                myAdvData = myAdvData.stream()
                        .sorted(Comparator.comparingInt(o -> Integer.parseInt(o.getState())))
                        .collect(Collectors.toList());

                if (advLvAdapter == null) {
                    advLvAdapter = new MyAdvLvAdapter(myAdvData, GovernmentMainActivity.this);
                    mineLv.setAdapter(advLvAdapter);
                } else {
                    advLvAdapter.resetData(myAdvData);
                    advLvAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context)
                        .load(o)
                        .into(imageView);
            }
        });

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //ignore
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rb1.setChecked(true);
                } else {
                    rb2.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //ignore
            }
        });

        mineLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Intent intent = new Intent(GovernmentMainActivity.this, GovAdvDetailActivity.class);
                intent.putExtra("id",myAdvData.get(position).getId());
                startActivity(intent);
            }
        });

        typeGv1.setOnItemClickListener(this);
        typeGv2.setOnItemClickListener(this);
    }

    @Override
    protected String getActionBarTitle() {
        return "政府服务热线";
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final int appealCategoryId = ((GovType.RowsBean) parent.getAdapter().getItem(position)).getId();


        final Intent intent;
        if (appealCategoryId == 23) {
            intent = new Intent(GovernmentMainActivity.this, GovernmentPubActivity.class);
            intent.putExtra("typeData", gson.toJson(typeData));
        } else {
            intent = new Intent(GovernmentMainActivity.this, GovernmentTypeActivity.class);
            final String appealCategoryName = ((GovType.RowsBean) parent.getAdapter().getItem(position)).getName();
            intent.putExtra("appealCategoryId", appealCategoryId);
            intent.putExtra("appealCategoryName", appealCategoryName);
        }
        startActivity(intent);
    }

    private static class TypeGvAdapter extends CommonAdapter<GovType.RowsBean, TypeViewHolder> {

        public TypeGvAdapter(List<GovType.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(GovType.RowsBean data, int position, TypeViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);

            viewHolder.serverName.setText(String.valueOf(data.getName()));
        }


        @Override
        protected TypeViewHolder getViewHolder() {
            return new TypeViewHolder();
        }

        @Override
        protected void initView(TypeViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

    private static class TypeViewHolder {
        ImageView im;
        TextView serverName;
    }

    class MyAdvLvAdapter extends CommonAdapter<GovAdvList.RowsBean, LvViewHolder> {

        public MyAdvLvAdapter(List<GovAdvList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(GovAdvList.RowsBean data, int position, LvViewHolder viewHolder, View convertView, Context context) {
            viewHolder.title.setText(data.getTitle());
            viewHolder.content.setText(data.getContent());
            viewHolder.tag1.setText("承办单位："+data.getUndertaker());
            viewHolder.tag2.setText("提交时间："+data.getCreateTime());
            viewHolder.tag3.setText("处理状态："+data.getState());

            Glide.with(convertView)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
        }

        @Override
        protected LvViewHolder getViewHolder() {
            return new LvViewHolder();
        }

        @Override
        protected void initView(LvViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }
    }

    private static class LvViewHolder {
        ImageView im;
        TextView title;
        TextView content;
        TextView tag1;
        TextView tag2;
        TextView tag3;
    }

//    private final class TypeRvAdapter extends RecyclerView.Adapter<TypeViewHolder>{
//
//        @NonNull
//        @Override
//        public TypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//            return new TypeViewHolder(
//                    LayoutInflater.from(GovernmentMainActivity.this).inflate(R.layout.server_list_item,parent,false)
//            );
//        }
//
//        @Override
//        public void onBindViewHolder(@NonNull TypeViewHolder holder, int position) {
//            final GovType.RowsBean rowsBean = typeData.get(position);
//
//        }
//
//        @Override
//        public int getItemCount() {
//            return typeData.size();
//        }
//    }

//    private static class TypeViewHolder extends RecyclerView.ViewHolder{
//        ImageView im;
//        TextView serverName;
//
//        public TypeViewHolder(@NonNull View itemView) {
//            super(itemView);
//
//        }
//    }

//    public static class NewsListAdapter extends CommonAdapter<NewsList.RowsBean, MineAdvViewHolder> {
//
//        public NewsListAdapter(List<NewsList.RowsBean> data, Context context) {
//            super(data, context);
//        }
//
//        @Override
//        protected void setData(NewsList.RowsBean data, int position, NewsListViewHolder viewHolder, View convertView) {
//            Glide.with(context)
//                    .load(Constant.BASE_URL + data.getCover())
//                    .into(viewHolder.im);
//            viewHolder.im.setVisibility(View.VISIBLE);
//            viewHolder.title.setText(String.valueOf(data.getTitle()));
//            viewHolder.content.setText(Html.fromHtml(data.getContent()));
//            viewHolder.tag1.setText(String.valueOf("评论总数:" + data.getCommentNum()));
//            viewHolder.tag3.setText(String.valueOf("发布日期：" + data.getPublishDate()));
//        }
//
//
//        @Override
//        protected NewsListViewHolder getViewHolder() {
//            return new NewsListViewHolder();
//        }
//
//        @Override
//        protected void initView(NewsListViewHolder viewHolder, View convertView) {
//            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
//            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
//            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
//            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
//            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
//            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
//        }
//    }
//
//    private static class MineAdvViewHolder {
//
//    }
}